package agent.ccs
import cwb.Value

//TODO(gustafa): Find symbol!
case class Prefix(act: CCSAction, valueSymbol: Option[Symbol]) {
  // Uses a value whenever the symbol is set
  def usesValues() : Boolean = !valueSymbol.isEmpty
  
  def prefixString(): String = act.mkstr()
  
 
  /**
   * toAct() returns the prefix's action
   * @return the prefix's action
   */
  def toAct(): CCSAction = act
  
  
  def le(rhs: Prefix): Boolean = {
    (valueSymbol, rhs.valueSymbol) match {
      case (None, Some(_)) => true
      case (Some(_), None) => false
      case (None, None) => act.le(rhs.act)
      case (Some(l), Some(r)) => l.name < r.name // false // TODO(Gustafa)  Lib.lexicographicLe (A.le, Value.symbolLe) ((a,x), (b,y))
    }
  }
  
  
  
  override
  def equals(rhs: Any): Boolean = {
    rhs match {
      case p: Prefix => p.asInstanceOf[Prefix].act == act
      case _ => false
    }
  }
  
  override
  // Hash code the same as that of the act.
  def hashCode() = act.hashCode()
}


object Prefix {
  /**
   * prefixFromAction creates a prefix from the given action.
   * @param act the action to be turned into a prefix.
   * @return the corresponding prefix.
   */
  def prefixFromAction(act: CCSAction): Prefix = new Prefix(act, None)
  // Todo(gustafa): Should be string option?
  def makePrefix(id: String, value: Option[Value]): Prefix = {
    val a = CCSAction.makeAction(id, value)
    if (a.isInput == Some(true)) {
      a.valueOf match {
        case Some(x) => new Prefix(a, Some(Symbol(x.valueOf))) // TODO(gustafa) - Sanity???
        case _ => new Prefix(a, None)
      }
    } else {
      new Prefix(a, None)
    }
  }
}
